package com.project.weimi.adapter;

import java.util.ArrayList;
import java.util.List;

import com.project.weimi.R;
import com.project.weimi.activity.AddressManagerActivity;
import com.project.weimi.activity.AddressManagerEditorActivity;
import com.project.weimi.activity.MainActivity;
import com.project.weimi.adapter.ShoppingAdapter.FooterViewHolder;
import com.project.weimi.entity.response.AddressEntity;
import com.project.weimi.widget.ToastMessage;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
/**
 * 地址管理适配
 * @author WUSC
 *
 */
public class AddressManagerAdapter extends RecyclerView.Adapter<ViewHolder> {
	private List<AddressEntity> list;
	private Context context;

	private View mHeaderView;

	private static final int TYPE_ITEM = 0;
	private static final int TYPE_FOOTER = 1;
	private static final int TYPE_HEADER = 2;

	public List<AddressEntity> getList() {
		return list;
	}

	public AddressManagerAdapter(Context context) {
		list = new ArrayList<AddressEntity>();
		this.context = context;
	}

	public void setHeaderView(View headerView) {
		mHeaderView = headerView;
		notifyItemInserted(0);
	}

	public View getHeaderView() {
		return mHeaderView;
	}

	@Override
	public int getItemCount() {
		return list.size() > 3 ? list.size() + 1 : list.size();
	}

	@Override
	public int getItemViewType(int position) {
		// if (position == 0 && mHeaderView != null) {
		// return TYPE_HEADER;
		// } else 
		if (position + 1 == getItemCount() && getItemCount() > 3) {
			return TYPE_FOOTER;
		} else {
			return TYPE_ITEM;
		}
	}

	@Override
	public void onBindViewHolder(final ViewHolder holder, final int position) {
		if (holder instanceof ItemViewHolder) {
			((ItemViewHolder) holder).address_delete.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					AlertDialog dialog = new AlertDialog.Builder(context).setMessage("确认要删除该地址吗？")
							.setNeutralButton("取消", null)
							.setPositiveButton("确定", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							list.remove(list.get(position));
							AddressManagerAdapter.this.notifyDataSetChanged();
							
							((ItemViewHolder) holder).address_delete.postDelayed(new Runnable() {
								@Override
								public void run() {
									ToastMessage.showToast(context, "删除成功");
								}
							}, 2000);
						}
					}).create();
					dialog.show();
				}
			});
			
			((ItemViewHolder) holder).address_editor.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					ToastMessage.showToast(context, "编辑" + (position));
					// 编辑
					Intent intent = new Intent(context, AddressManagerEditorActivity.class);
					Bundle bundle = new Bundle();
					bundle.putSerializable("address", list.get(position));
					bundle.putInt("states", 1); // 编辑
					bundle.putInt("position", position); // position
					intent.putExtras(bundle);
					
					if(context instanceof AddressManagerActivity)
						((AddressManagerActivity) context).startActivityForResult(intent, 1);
				}
			});
			
			((ItemViewHolder) holder).address_check.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					itemCheckBox(position);

					((ItemViewHolder) holder).address_check.setChecked(list.get(position).isAddress_check());
					
					AddressManagerAdapter.this.notifyDataSetChanged();
				}
			});
			
			((ItemViewHolder) holder).address_check.setChecked(list.get(position).isAddress_check());

			((ItemViewHolder) holder).address_address.setText(list.get(position).getAddress_area() + list.get(position).getAddress_jiedao() + list.get(position).getAddress_address());
			
			((ItemViewHolder) holder).address_name.setText(list.get(position).getAddress_name());
			
			if (!TextUtils.isEmpty(list.get(position).getAddress_phone())
					&& !TextUtils.equals(list.get(position).getAddress_phone(), "null")) {
				((ItemViewHolder) holder).address_phone.setText(list.get(position).getAddress_phone());
			}

		}
	}

	@Override
	public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

		if (viewType == TYPE_ITEM) {
			View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_address_manager_item, null);
			view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
			return new ItemViewHolder(view);
		} else if (viewType == TYPE_FOOTER) {
			View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.shopping_footerview, null);
			view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
			return new FooterViewHolder(view);
		}
		return null;
	}
	
	class FooterViewHolder extends ViewHolder {
		public FooterViewHolder(View view) {
			super(view);
		}
	}

	class ItemViewHolder extends ViewHolder {
		TextView address_editor;
		TextView address_delete;
		CheckBox address_check;
		TextView address_address;
		TextView address_phone;
		TextView address_name;

		public ItemViewHolder(View view) {
			super(view);
			address_editor = (TextView) view.findViewById(R.id.address_editor);
			address_check = (CheckBox) view.findViewById(R.id.address_check);
			address_name = (TextView) view.findViewById(R.id.address_name);
			address_delete = (TextView) view.findViewById(R.id.address_delete);
			address_address = (TextView) view.findViewById(R.id.address_address);
			address_phone = (TextView) view.findViewById(R.id.address_phone);
		}
	}
	
	private void itemCheckBox(int position) {
		// TODO Auto-generated method stub
		for (int i = 0; i < list.size(); i++) {
			if(i == position){
				list.get(i).setAddress_check(true);
			} else {
				list.get(i).setAddress_check(false);
			}
		}
	}

}
